package com.uchengwang.ui.base_ui.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.uchengwang.R;
import com.uchengwang.functions.db.manager.LiteOrmDBUtil;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.login.ui.CompleteInfoActivity;
import com.uchengwang.ui.cm_ui.login.ui.IntroduceActivity;
import com.uchengwang.ui.cm_ui.login.ui.LoginActivity;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.cm_ui.umsg.ui.ClubMainActivity;
import com.uchengwang.ui.utils.AppStatusManager;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.StatusBarUtil;

import java.lang.ref.WeakReference;

/**
 * Created by U城网 on 2017/7/17.
 */

public class SlpashActivity extends AllBaseActivity{

    private ImageView imageView;
    private MyHandler handler ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        AppStatusManager.getInstance().setAppStatus(AppStatusManager.STATUS_NORMAL); //进入应用 设置应用状态
        super.onCreate(savedInstanceState);

        StatusBarUtil.getUtil().setStatusBarColor(this, R.color.divider_bg);
        StatusBarUtil.getUtil().StatusBarLightMode(this);

        handler = new MyHandler(this);
        if (!SPUtils.getPreferences(this).isOpend()){
            SPUtils.getPreferences(this).setOpened();
            Intent intent = new Intent(this, IntroduceActivity.class);
            startActivity(intent);
            this.finish();
        }else {
            setContentView(R.layout.slpash_main);
            initView();
        }
    }

    private void initView(){
        imageView = (ImageView) findViewById(R.id.main_image);
        ImageManager.getInstance().loadImage(this, imageView, R.drawable.image_slpash, ImageManager.ImageType.NARMOL);

        handler.sendEmptyMessageDelayed(0, 2000);
    }

    /**
     * 取出本地token 取出上次存放token的时间 判断时间有没有过期 5天  如果时间过期 跳转到登录界面 不然跳转主界面
     * */
    private String userAvatar, nickName, token, phoneS;
    private long uid, tokenTime;
    private void initData() {
        uid = SPUtils.getPreferences(this).getAccountId();
        phoneS = SPUtils.getPreferences(this).getUserName();
        UChengApplication.userRole = SPUtils.getPreferences(this).getInt(BaseConfig.USER_ROLE);
        userAvatar = SPUtils.getPreferences(this).getString(BaseConfig.USER_AVATAR);
        nickName = SPUtils.getPreferences(this).getString(BaseConfig.NICK_NAME);

        token = SPUtils.getPreferences(this).getToken();
        tokenTime = SPUtils.getPreferences(this).getTokenTime();

        UChengApplication.TOKEN = token;
        UChengApplication.uid = uid;
        Log.d("Title", "获取SysTitme="+System.currentTimeMillis());

        if (uid <1 || TextUtils.isEmpty(token)){
            toLogin(true);
        }else {
            int num = (int) ((System.currentTimeMillis()-tokenTime)/(1000*60*60*24));
            if (num <=3){
                //token 在有效期
                if (TextUtils.isEmpty(nickName) || TextUtils.isEmpty(userAvatar)){
                    //注册了 但是没有完善资料
                    startActivity(new Intent(this, CompleteInfoActivity.class));
                    SlpashActivity.this.finish();
                }else {
                    toLogin(false);
                }
            }else {
                //token失效 获取新token
                HttpUserManager.getManager().getNewToken(this, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if(stutas !=403){
                            //token没有过期的情况  token无效或者token过期时 会有个弹窗出来
                            toLogin(false);
                        }
                    }
                });

            }
        }
    }

    /**
     * @param isToLogin true 说明该用户信息都为空 需要去登录
     * */
    private void toLogin(boolean isToLogin){
        Intent intent = null;


        if (!isToLogin){
            if (UChengApplication.userRole ==2){
                UChengApplication.clubId = uid;
                intent = new Intent(this, ClubMainActivity.class);
            }else {
                UChengApplication.uid = uid;
                intent = new Intent(this, MainActivity.class);
            }

            UChengApplication.userAvatar = userAvatar;
            UChengApplication.nickName = nickName;
            UChengApplication.uniId = SPUtils.getPreferences(this).getLong(BaseConfig.UNI_ID);
            UChengApplication.myUniId = SPUtils.getPreferences(this).getLong(BaseConfig.UNI_ID);

            UChengApplication.uniDataId = SPUtils.getPreferences(this).getLong(BaseConfig.UNI_DATA_ID);

            UChengApplication.schoolName = SPUtils.getPreferences(this).getString(BaseConfig.UNI_NAME);
            UChengApplication.mySchoolName = SPUtils.getPreferences(this).getString(BaseConfig.UNI_NAME);
            UChengApplication.isAuthen = SPUtils.getPreferences(this).getBoolean(BaseConfig.IS_AUTHEN);
            UChengApplication.sex = SPUtils.getPreferences(this).getInt(BaseConfig.SEX);

            LiteOrmDBUtil.getLiteOrm(SlpashActivity.this);

        }else {
            intent = new Intent(this, LoginActivity.class);
        }

        startActivity(intent);
        this.finish();
    }

    private static class MyHandler extends Handler{
        private WeakReference<SlpashActivity> weakReference;
        public MyHandler (SlpashActivity activity){
            weakReference = new WeakReference<SlpashActivity>(activity);

        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    SlpashActivity activity = weakReference.get();
                    activity.initData();
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null){
            handler.removeCallbacksAndMessages(null);
        }
    }
}
